package com.xy.network.observer

import com.xy.baselibrary.base.MvvmBaseModel
import com.xy.baselibrary.base.MvvmNetworkObserver
import com.xy.network.errorhandler.ExceptionHandle
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * @ClassName BaseObserver
 * @Author xy
 * @Date 2021/8/17 14:46
 * @Description rxJava 观察者模式 提取出 成功和失败的方法
 */
abstract class OneBaseObserver<T> : Observer<T> {
    lateinit var mvvmBaseModel:MvvmBaseModel<T>
    lateinit var mvvmNetworkObserver: MvvmNetworkObserver<T>

    override fun onSubscribe(d: Disposable) {
        mvvmBaseModel.addDisposable(d)
    }

    override fun onNext(t: T) {
//        onSuccess(t)
        mvvmNetworkObserver.onSuccess(t,false) //不来自缓存
    }

    override fun onError(e: Throwable) {
//        onFailure(e)
        if (e is ExceptionHandle.ResponseException){
            mvvmNetworkObserver.onFailure(e)
        }else{
            mvvmNetworkObserver.onFailure(ExceptionHandle.ResponseException(e,ExceptionHandle.ERROR.UNKNOWN))
        }

    }

    override fun onComplete() {

    }

    abstract fun onSuccess(t: T)
    abstract fun onFailure(e: Throwable)
}